//
//  VisitorView.swift
//  sinaWeiBo
//
//  Created by Xuwei Yang on 16/8/8.
//  Copyright © 2016年 Xuwei Yang. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView {
    
    var loginClosure: (()->())?
    // MARK:-懒加载
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //  罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //  主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    //消息信息
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        //  设置文字居中
        label.textAlignment = .Center
        //  多行显示
        label.numberOfLines = 0
        return label
    
    }()

    //注册
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        button.addTarget(self, action: #selector(VisitorView.registerButtonAction), forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    //登录
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: #selector(VisitorView.loginButtonAction), forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func startAnimation() {
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    //修改访客视图信息
    func updateVisitorViewInfo(message: String?, imageName: String?) {
        if let msg = message, imgName = imageName {
        
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            cycleImageView.hidden = true
        }else{
            
            cycleImageView.hidden = false
            startAnimation()
        }
    }
    
    //设置UI
    private func setupUI() {
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        //添加控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置约束
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.height.equalTo(35)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }

    }

    
    //  MARK: --添加点击事件
    @objc private func registerButtonAction() {
        print("注册")
        //  执行闭包
        loginClosure?()
        
    }
    
    @objc private func loginButtonAction() {
        print("登录")
        //  执行闭包
        loginClosure?()
        
    }
    
}

